﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ArchivingDataModel;
using ArchivingDataWebForms.Helpers;

namespace ArchivingDataWebForms.Pages.Attachments
{
    public partial class EditAttachmentPhoto : WebFormBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.Request.QueryString[QueryStringParams.Attachment_ID]))
                {
                    using (ArchivingDataEntities ent = new ArchivingDataEntities())
                    {
                        long id = Convert.ToInt64(this.Request.QueryString[QueryStringParams.Attachment_ID]);
                        var item = ent.Attachments.FirstOrDefault(i => i.Attachment_ID == id);
                        this.RadImageEditorAttachment.ImageUrl = item.Attachment_Path;
                        this.RadTextBoxOCR.Text = item.Ocr;
                    }
                }
            }
            catch
            {

            }
            if (!this.IsPostBack)
            {
            }
        }

        protected void RadImageEditorAttachment_ImageSaving(object sender, Telerik.Web.UI.ImageEditorSavingEventArgs e)
        {
            try
            {
                e.Cancel = true;
                if (!string.IsNullOrEmpty(this.Request.QueryString[QueryStringParams.Attachment_ID]))
                {
                    using (ArchivingDataEntities ent = new ArchivingDataEntities())
                    {
                        long id = Convert.ToInt64(this.Request.QueryString[QueryStringParams.Attachment_ID]);
                        var item = ent.Attachments.FirstOrDefault(i => i.Attachment_ID == id);
                        item.FileReference.Load();
                            //add versioning
                        AttachmentsVersioning versioning = new AttachmentsVersioning
                        {
                            Version_Date=DateTime.Now,
                            Attachment_Code = item.Attachment_Code,
                            Attachment_Order = item.Attachment_Order,
                            Attachment_UniqueText = item.Attachment_UniqueText,
                            AttachmentNote_Ar = item.AttachmentNote_Ar,
                            AttachmentNote_En = item.AttachmentNote_En,
                            AttachmentTitle_Ar = item.AttachmentTitle_Ar,
                            AttachmentTitle_En = item.AttachmentTitle_En,
                            Customer_ID = item.Customer_ID,
                            File_ID = item.File.File_ID,
                            IS_Encrypted = item.IS_Encrypted,
                            MimeType = item.MimeType,
                            OCR = item.Ocr,
                        };
                        item.AttachmentsVersionings.Add(versioning);
                        ent.SaveChanges();
                        string datePath = DateTime.Now.ToString("yyyy/MM/dd", CultureInfo.InvariantCulture);
                        string versionPath = CustomAppSettings.AttachmentsVersioning + "/" + datePath + "/" + versioning.Version_ID + Path.GetExtension(CustomAppSettings.getActualPath(item.Attachment_Path));
                        Directory.CreateDirectory(Path.GetDirectoryName(CustomAppSettings.getActualPath(versionPath)));
                        System.IO.File.Copy(CustomAppSettings.getActualPath(item.Attachment_Path), CustomAppSettings.getActualPath(versionPath));
                        versioning.Version_Path = versionPath;

                        string imagePath = CustomAppSettings.FilesPathes + "/" + datePath + "/" + item.Attachment_ID + Path.GetExtension(CustomAppSettings.getActualPath(item.Attachment_Path));
                        Directory.CreateDirectory(Path.GetDirectoryName(CustomAppSettings.getActualPath(imagePath)));
                        e.Image.Image.Save(CustomAppSettings.getActualPath(imagePath));
                        item.Attachment_Path = imagePath;
                        ent.SaveChanges();
                        this.ShowNotification("تم الحفظ بنجاح", NotificationIconType.Ok);
                    }
                }
            }
            catch
            {

            }
        }

        protected void RadImageEditorAttachment_DialogLoading(object sender, Telerik.Web.UI.ImageEditorDialogEventArgs e)
        {

        }
    }
}